package be.rhok.eenzaamheidswijzer.facade;

import be.rhok.eenzaamheidswijzer.dto.QuestionDto;
import be.rhok.eenzaamheidswijzer.dto.SolutionDto;
import be.rhok.eenzaamheidswijzer.dto.StartTestDto;
import be.rhok.eenzaamheidswijzer.model.Question;
import be.rhok.eenzaamheidswijzer.model.Solution;
import be.rhok.eenzaamheidswijzer.service.LonelyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class LonelyFacadeImpl implements LonelyFacade {

    @Resource
    private LonelyService lonelyService;

    @Override
    public StartTestDto startTest(String ip) {
        Long token = lonelyService.createNewUser(ip);
        StartTestDto startTest = new StartTestDto();
        startTest.setToken(token);
        startTest.setQuestion(getQuestion(1l));
        return startTest;
    }

    @Override
    public QuestionDto getQuestion(Long questionId, Long token, Long answerId) {
        lonelyService.logAnswer(token, answerId);
        return getQuestion(questionId);
    }

    private QuestionDto getQuestion(Long questionId) {
        Question question = lonelyService.getQuestion(questionId);
        return new QuestionDto(question);
    }

    @Override
    public SolutionDto getSolution(Long solutionId, Long token, Long answerId) {
        lonelyService.logAnswer(token, answerId);
        Solution solution = lonelyService.getSolution(solutionId);
        lonelyService.logSolution(token, solution);
        return new SolutionDto(solution);
    }
}